/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.oneboot.core.mvc.base;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class AuthUser implements UserDetails {
	private final String id;
	private final String username;
	private final String password;
	private final Collection<? extends GrantedAuthority> authorities;
	private final Date lastPasswordResetDate;
	/** 最后登录失败时间 */
	@SuppressWarnings("unused")
	private final Date lastFailDt;
	private final List<String> roleLsit;
	private final List<? extends Object> roles;

	private final BaseAuthUser user;

	/**  */
	private static final long serialVersionUID = 8890097291353714011L;

	public AuthUser(String id, String username, String password, String email,
			Collection<? extends GrantedAuthority> authorities, Date lastPasswordResetDate, Date lastFailDt,
			BaseAuthUser user, List<String> roleLsit, List<? extends Object> roles) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.authorities = authorities;
		this.lastPasswordResetDate = lastPasswordResetDate;
		this.lastFailDt = lastFailDt;
		this.user = user;
		this.roleLsit = roleLsit;
		this.roles = roles;
	}

	public List<? extends Object> getRoles() {
		return roles;
	}

	public BaseAuthUser getUser() {
		return user;
	}

	public List<String> getRoleLsit() {
		return roleLsit;
	}

	/**
	 * 返回分配给用户的角色列表
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@JsonIgnore
	public String getId() {
		return this.id;
	}

	public String getSalt() {
		return "salt" + this.id;
	}

	/**
	 */
	@Override
	@JsonIgnore
	public String getPassword() {
		return this.password;
	}

	/**
	 */
	@Override
	@JsonIgnore
	public String getUsername() {
		return this.username;
	}

	/**
	 * 账户是否未过期
	 * 
	 */
	@Override
	@JsonIgnore
	public boolean isAccountNonExpired() {
		return true;
	}

	/**
	 * 账户是否未锁定
	 * 
	 */
	@Override
	@JsonIgnore
	public boolean isAccountNonLocked() {
		if ("2".equals(user.getState())) {
			return false;
		}
		return true;
	}

	/**
	 * 密码是否未过期
	 * 
	 */
	@Override
	@JsonIgnore
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/**
	 * 账户是否激活
	 * 
	 */
	@Override
	@JsonIgnore
	public boolean isEnabled() {
		return true;
	}

	/**
	 * 这个是自定义的，返回上次密码重置日期
	 * 
	 * @return
	 */
	@JsonIgnore
	public Date getLastPasswordResetDate() {
		return lastPasswordResetDate;
	}

	public String getUserId() {
		return id;
	}

	public boolean isAdmin() {
		return false;
	}
}
